package com.example.springdemo.service.content.impl;

import com.example.springdemo.dao.share.ShareMapper;
import com.example.springdemo.datasource.DB;
import com.example.springdemo.datasource.DataSource;
import com.example.springdemo.datasource.DataSourceContextHolder;
import com.example.springdemo.domain.entity.share.Share;
import com.example.springdemo.service.content.ShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author wanghuajun
 * @version 1.0
 * @date 2019-08-09 21:33
 */
@Slf4j
@Service
public class ShareServiceImpl implements ShareService {

    @Autowired
    private ShareMapper shareMapper;

    @Transactional(rollbackFor = Exception.class)
    @DataSource(name = DB.CONTENT_CENTER)
    @Override
    public Share getShareById(Integer id) {
        log.info("ShareServiceImpl#getShareById数据源为---------------->dataSource:{}", DataSourceContextHolder.getDataSourceType());
        Share share = Share.builder()
                .id(id)
                .author("李易峰")
                .title("你好China")
                .updateTime(new Date())
                .downloadUrl("www.baidu.com")
                .build();

        shareMapper.updateByPrimaryKeySelective(share);

        // int i = 1/0;
        return shareMapper.selectByPrimaryKey(id);
    }

}
